const webpack 						= require('webpack');
const HtmlWebpackPlugin 			= require('html-webpack-plugin');
const glob = require('glob');
const path = require('path');

const setMPA = () => {
	const entry = {};
	const historyApi = {};
  	const htmlWebpackPlugins = [];
  	const entryFiles = glob.sync(path.resolve(__dirname, '../src/*/main.js'));
  	for(let item of entryFiles){
  		const pageName = item.match(/src\/(.*)\/main.js/)[1];
	    entry[pageName] = item;
	    historyApi[pageName] = `/${pageName}.html`;
	    htmlWebpackPlugins.push(
	    	new HtmlWebpackPlugin({
	    		chunks:[pageName],
				filename:`${pageName}.html`,
				template:path.resolve(__dirname,`../src/${pageName}/index.html`),
			}),
	    )
  	}
    return {
    	historyApi,
    	entry,
    	htmlWebpackPlugins
  	}
}

const { historyApi,entry, htmlWebpackPlugins } = setMPA();

const options = {
	entry,
	output:{
		chunkFilename: 'static/js/[name].[chunkhash:10].min.js',
    	filename: 'static/js/[name].[hash:10].min.js',
    	publicPath:'/activity/',
    	path:path.resolve(__dirname,'../activity')
	},
	devtool: 'eval-source-map',
	module:{
		rules:[
			{
				test: /\.m?js$/,
				include: path.resolve(__dirname,'../src'),
				exclude: /node_modules/,
				use:[
					{
						loader:'babel-loader',
					}
				]
			}
		]
	},
	plugins:[
		new webpack.HotModuleReplacementPlugin(),
	].concat(htmlWebpackPlugins),
	resolve:{
		extensions:['.js'],
		alias:{
			// 'vue': 'vue/dist/vue.esm.js',
			'@':path.resolve(__dirname,'../src')
		}
	}
};
module.exports = options;